package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.TeamDetailActivity;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.event.JoinOrQuitTeamEvent;
import reduce.fz.com.reduce.event.KeepEvent;
import reduce.fz.com.reduce.event.PubDynamicEvent;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.TeamDynamicUtil;

/**
 * team详情我的页签名
 */
public class TeamMeFragment extends BaseViewPagerFragment implements View.OnClickListener {

    //private TextView tvPubDynamic;
    //private TextView tvWitnessFriend;
    private FrameLayout mMyDynamic;
    private Team team;
    private LinearLayout mLlJoined;
    private RelativeLayout mNotJoined;
    private TextView mTvJoin;
    private TeamDetailActivity mActivity;
    private TeamMeDynamicListFragment meDynamicFragment;
    private TeamService teamService = ServiceBuilder.builderTeamService(null);

    private static final int GET_KEEP_DAYS = 110;

    public static TeamMeFragment create(Team team, int position) {
        TeamMeFragment fragment = new TeamMeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("team", team);
        bundle.putInt(BUNDLE_FRAGMENT_INDEX, position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        team = (Team) getArguments().getSerializable("team");
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    public void onEvent(KeepEvent e) {
    }

    public void initDisply() {
            mLlJoined.setVisibility(View.VISIBLE);
            mNotJoined.setVisibility(View.GONE);
    }

    public void initWidget(View view) {
        mLlJoined = (LinearLayout) view.findViewById(R.id.ll_joined);
        mNotJoined = (RelativeLayout) view.findViewById(R.id.rl_no_joined);
        mMyDynamic = (FrameLayout) view.findViewById(R.id.fl_my_dynamic_container);
        mTvJoin = (TextView) view.findViewById(R.id.tv_join_btn);
        mTvJoin.setOnClickListener(this);
        initDisply();
    }

    public void attachFragment() {
        User user=UserPreference.getUser();
        meDynamicFragment = TeamMeDynamicListFragment.create(team.teamid,user.uid);
        getChildFragmentManager().beginTransaction().replace(R.id.fl_my_dynamic_container, meDynamicFragment).commit();

    }


    public TeamMeFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_team_me, container, false);
        mActivity= (TeamDetailActivity) getActivity();
        initWidget(view);
        attachFragment();
        return view;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case (GET_KEEP_DAYS):
                break;
            default:
                break;
        }

    }
    /**
     * 监听按钮
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_join_btn:
                //joinTeam();
            default:
                break;

        }

    }

    /**
     * 加入小组
     */
    public void joinTeam() {
        teamService.joinTeam(team.teamid, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
        }) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<HashMap> data) {
                team.teamAttach.isJoin = true;
                mLlJoined.setVisibility(View.VISIBLE);
                mNotJoined.setVisibility(View.GONE);
                User user= UserPreference.getUser();
                mActivity.displayPub(true,true,false,false);
                mActivity.displayBtn();
                EventBus.getDefault().post(new JoinOrQuitTeamEvent(team,JoinOrQuitTeamEvent.JOIN));
                EventBus.getDefault().post(new PubDynamicEvent(TeamDynamicUtil.joinTeam(user,team.teamid)));
            }
        });

    }

    @Override
    public boolean isViewBeingDragged(MotionEvent event) {
        return meDynamicFragment.isViewBeingDragged(event);
    }
}
